package com.yueshang.neuron.modular.system.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yueshang.common.entity.ResultResponse;
import com.yueshang.common.utils.ToolUtil;
import com.yueshang.framework.constant.factory.PageFactory;
import com.yueshang.neuron.common.enums.ResultCodeEnum;
import com.yueshang.neuron.common.util.InfiniteLevelTreeUtil;
import com.yueshang.neuron.common.util.Node;
import com.yueshang.neuron.core.warpper.UserWarpper;
import com.yueshang.neuron.modular.system.api.IDeptService;
import com.yueshang.neuron.modular.system.model.Dept;
import com.yueshang.neuron.modular.system.model.User;
import com.yueshang.neuron.modular.system.vo.res.node.ZTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 部门管理入口
 * @auton  shililu
 * @data 2018-08-30
 */
@RestController
@RequestMapping("/v1/dept")
public class DeptController extends ApiController {

    @Autowired
    private IDeptService deptService;

    /**
     * 添加部门
     * 名称重复
     *  必传
     * @param dept
     * @return
     */
    @PostMapping
    public ResultResponse addUser(@RequestBody Dept dept) {
        //插入之前先判断父及id是否存在

        deptSetPids(dept);
        //插入之前先验证部门名称是否重复
        deptService.save(dept);
        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode());
    }

    /**
     * 更新部门
     *
     * @param dept
     * @return
     */
    @PutMapping
    public ResultResponse editUser(@RequestBody Dept dept)  {
        deptService.updateById(dept);
        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode());
    }

    /**
     * 删除部门
     *
     * @param id 删除部门通过id
     * @return
     */
    @DeleteMapping
    public ResultResponse delUser(Long id) {
        deptService.removeById(id);
        return ResultResponse.ok(ResultCodeEnum.OPERATION_SUCCESS.getCode());
    }

    @GetMapping("/depts")
    public Object list() {
        return ResultResponse.ok(ResultCodeEnum.GAIN_SUCCESS.getCode(), ResultCodeEnum.GAIN_SUCCESS.getMsg(), deptService.list(new QueryWrapper<>()));
    }

    /**
     * 分页获取用户列表
     */
    @GetMapping("/tree")
    public ResultResponse tree() {
        List<Node> nodeList=deptService.getNodeList("");
        List<Node> reNodeList=InfiniteLevelTreeUtil.getInfiniteLevelTree(nodeList);
        return ResultResponse.ok(ResultCodeEnum.GAIN_SUCCESS.getCode(),ResultCodeEnum.GAIN_SUCCESS.getMsg(),reNodeList);
    }

    /**
     * 分页树 排除自己及子
     */
    @GetMapping("/treeParent")
    public ResultResponse treeParent(Long nodeId) {
        List<Node> nodeList=deptService.getParentNodeList(nodeId);
        List<Node> reNodeList=InfiniteLevelTreeUtil.getInfiniteLevelTree(nodeList);
        return ResultResponse.ok(ResultCodeEnum.GAIN_SUCCESS.getCode(),ResultCodeEnum.GAIN_SUCCESS.getMsg(),reNodeList);
    }

    private void deptSetPids(Dept dept) {
        if (ToolUtil.isEmpty(dept.getPid()) || dept.getPid().equals(0)) {
            dept.setPid("0");
            dept.setPids("[0],");
        } else {
            String pid = dept.getPid();
            Dept temp = deptService.getById(pid);
            String pids = temp.getPids();
            dept.setPid(pid);
            dept.setPids(pids + "[" + pid + "],");
        }
    }
}
